#pragma once
#include<iostream>
#include<pthread.h>
#include<string>

using namespace std;

typedef void(*callback_t)();
static int num = 1;


class Thread
{
public:
    Thread(callback_t cb):cb_(cb),isrunning_(false)
    {}

    static void* Routine(void* args)
    {
        Thread* td=static_cast<Thread*>(args);
        td->Enter();
        return nullptr;
    }
    void Run()
    {
        name_="thread-"+to_string(num++);
        start_timestamp_=time(nullptr);
        isrunning_=true;
        pthread_create(&tid_,nullptr,Routine,this);
    }
    void Join()
    {
        pthread_join(tid_,nullptr);
        isrunning_=false;
    }
    string Name()
    {
        return name_;
    }
    uint64_t StartTimestamp()
    {
        return start_timestamp_;
    }
    bool IsRunning()
    {
        return isrunning_;
    }
    void Enter()
    {
        cb_();
    }
    ~Thread()
    {}
private:
    pthread_t tid_;
    string name_;
    uint64_t start_timestamp_;
    bool isrunning_;

    callback_t cb_;//传入的无参无返回值的函数
};

